package shizhan;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class SimpleBeanConvertor {
    public static <T> T convert(HttpServletRequest request,Class<T> targetType) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        Map<String,String[]>params  = request.getParameterMap();
        T target=targetType.newInstance();
        if (params.size()==0){
            Method [] allMethods= targetType.getMethods();
            if (allMethods!=null&&allMethods.length>0){
                for (Method method:allMethods){
                    if (method.getName().startsWith("set")){
                        Class []args=method.getParameterTypes();
                        if (args.length==1){
                            String paramName=method.getName().substring(3,4).toLowerCase()+method.getName().substring(4);
                            if (params.containsKey(paramName)) {
                                try {
                                    Object value = parseValue(params.get(paramName),args[0]);
                                }catch (ParseException | ClassNotFoundException | UnsupportedTypeException e){
                                    e.printStackTrace();
                                }
                            }else if (Boolean.class==args[0]||boolean.class==args[0]){
                                method.invoke(target,false);
                            }
                        }
                    }
                }
            }
        }
        return target;
    }
    /**
     * 将HTTP请求中String类型的参数值转换为相关属性的类型
     * 支持String、yyyy-MM-dd格式的日期、基本类型及包装类、BigDecimal、BigInteger
     * 修改此方法可添加对更多类型的支持
     * @param value HTTP请求参数值
     * @return 转换后的参数值
     */
    public static Object parseValue(String []value,Class type) throws ParseException, ClassNotFoundException, UnsupportedTypeException {
        if (String.class==type){
            return value[0];
        }
        if (java.util.Date.class==type){
            return new SimpleDateFormat("yyyy-MM-dd").parse(value[0]);
        }
        if (boolean.class==type||Boolean.class==type){
            return true;
        }
        if (char.class==type||Character.class==type){
            return value[0].charAt(0);
        }
        if (int.class==type||Integer.class==type){
            return Integer.valueOf(value[0]);
        }
        if (short.class==type||byte.class==type||long.class==type||float.class==type||double.class==type){
                try {
                    type=Class.forName("java.lang."+type.getName().substring(0,1).toUpperCase()+type.getName().substring(1));
                }catch (ClassNotFoundException e){
                    e.getException();
                }
                if (type.getName().startsWith("java.lang.")||type.getName().startsWith("java.math.")){
                    try {
                        return type.getConstructor(String.class).newInstance(value[0]);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
        }
        throw new UnsupportedTypeException();
    }
    public static class  UnsupportedTypeException extends Exception{
        public  UnsupportedTypeException(){
            super("不支持的类型转换");
        }
        public UnsupportedTypeException(Throwable e){
            super("不支持的类型转换",e);
        }
    }
}
